<!--
文件名: Login.vue
作者: Trae AI
创建日期: 2024-02-20
描述: 教务系统登录页面组件，包含用户名密码表单和登录功能
-->
<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">教务系统登录</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        @submit.prevent="handleLogin"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" class="login-btn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance } from 'element-plus'

// 路由实例
const router = useRouter()

// 表单引用
const loginFormRef = ref<FormInstance | null>(null)

// 登录表单数据 - 使用 reactive 确保响应式属性
const loginForm = reactive({
  username: '',
  password: '',
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: ['blur', 'change'] },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
}

// 登录方法
const handleLogin = () => {
  if (!loginFormRef.value) return
  loginFormRef.value.validate((valid) => {
    if (valid) {
      // 模拟登录请求
      setTimeout(() => {
        ElMessage.success('登录成功')
        // 获取重定向路径，默认跳转到首页
        const redirectPath = router.currentRoute.value.query.redirect || '/'
        // 添加调试日志
        console.log('准备跳转到:', redirectPath)
        router.push(redirectPath as string).catch(err => {
          console.error('路由跳转失败:', err)
        })
      }, 500)
    } else {
      ElMessage.error('请填写正确的用户名和密码')
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  overflow: hidden;
  background: url('@/assets/images/playground79.jpg') no-repeat center center;
  background-size: cover;
  position: relative;
}

.login-card {
  width: 400px;
  padding: 40px;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.95); /* 使用轻微透明的背景 */
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative; /* 确保卡片在遮罩层上方 */
  z-index: 1;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.2); /* 进一步降低遮罩层透明度 */
}



.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.el-form-item {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
  background-color: #409eff;
  border-color: #409eff;
}

.login-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}
</style>
